//
//  GoodTalk.swift
//  NaiCha
//
//  Created by 唐安坤 on 2024/8/28.
//

import SwiftUI

struct GoodTalk: View {
    @State var totalTip = "鲜有好评"
    @State var totalNum: Int = 0
    @State var totalColor: Color = .blue
    var body: some View {
        
        VStack(spacing: 20){
            HStack{
                TopButton(totalTip: $totalTip, totalNum: $totalNum, totalColor: $totalColor)
            }
            HStack{
                MyButton(totalTip: $totalTip, totalNum: $totalNum, totalColor: $totalColor, tip: "第1课好评数量")
            }
            HStack{
                MyButton(totalTip: $totalTip, totalNum: $totalNum, totalColor: $totalColor, tip: "第2课好评数量")
            }
            HStack{
                MyButton(totalTip: $totalTip, totalNum: $totalNum, totalColor: $totalColor, tip: "第3课好评数量")
            }
        }
    }
}

#Preview {
    GoodTalk()
}

private struct TopButton: View {
    
    @Binding var totalTip: String
    @Binding var totalNum: Int
    @Binding var totalColor: Color
    
    var body: some View {
        Button {
            totalNum += 1
            if totalNum > 7 {
                totalTip = "好评如潮"
                totalColor = .red
            }else{
                totalTip = "鲜有好评"
                totalColor = .blue
            }
        } label: {
            HStack(spacing: 10){
                Text("总好评数量:\(totalNum),\(totalTip)").font(.system(size: 20))
            }
            .foregroundColor(.white)
            .padding()
        }
        .background(totalColor)
        .cornerRadius(15)
        .shadow(color: .gray, radius: 5, x: /*@START_MENU_TOKEN@*/0.0/*@END_MENU_TOKEN@*/, y: 5.0)
    }
}
private struct MyButton: View {
    @Binding var totalTip: String
    @Binding var totalNum: Int
    @Binding var totalColor: Color
    @State var tip: String
    @State var num: Int = 0
    @State var bgColor: Color = .blue
    var body: some View {
        Button {
            num += 1
            totalNum += 1
            bgColor = num > 5 ? .red : .blue
            totalTip = totalNum > 7 ? "好评如潮" : "鲜有好评"
            totalColor = totalNum > 7 ? .red : .blue
        } label: {
            HStack(spacing: 10){
                Image(systemName: "star.circle").font(.system(size: 20))
                Text("\(tip):\(num)").font(.system(size: 20))
            }
            .foregroundColor(.white)
            .padding()
        }
        .background(bgColor)
        .cornerRadius(15)
        .shadow(color: .gray, radius: 5, x: /*@START_MENU_TOKEN@*/0.0/*@END_MENU_TOKEN@*/, y: 5.0)
    }
}
